<template>
  <div class="admin">
    <el-form label-width="80px" ref="form" :rules="rules" :model="form">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" :maxlength="11"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="handleSave" :loading="isSave">保存</el-button>
  </div>
</template>
<script>
  import {mapState, mapActions} from 'vuex'
  export default {
    data () {
      return {
        appid: this.$route.params.appid,
        form: {
          mobile: null,
          name: null
        },
        isSave: false,
        rules: {
          mobile: [{required: true, message: '请输入手机号码', trigger: 'blur'},
            {pattern: /^\d{11}$/, message: '请输入正确的手机号码', trigger: 'blur'}],
          name: [{required: true, message: '请输入姓名', trigger: 'blur'},
            {pattern: /(^[\u4e00-\u9fa5]{1}[\u4e00-\u9fa5\\.·]{0,20}[\u4e00-\u9fa5]{1}$)|(^[a-zA-Z]{1}[a-zA-Z\s]{0,20}[a-zA-Z]{1}$)/, message: '姓名格式不对，应为：张三，张·三，James，James Harden', trigger: 'blur'}]
        }
      }
    },
    mounted () {
      this.initPage()
    },
    computed: {
      ...mapState({
        wxDetail: state => state.wechatManage.wechatDetail
      })
    },
    methods: {
      ...mapActions(['setAdminInfo', 'getWechatInfo']),
      async initPage () {
        await this.getWechatInfo(this.appid)
        this.form.mobile = this.wxDetail.adminPhone
        this.form.name = this.wxDetail.adminName
      },
      async handleSave () {
        if (!/^1(3|5|7|8)\d{9}$/.test(this.form.mobile)) {
          this.$message.warning('请输入正确的手机号码')
          return
        }
        this.isSave = true
        const res = await this.setAdminInfo({
          appid: this.appid,
          phone: this.form.mobile,
          name: this.form.name
        })
        this.isSave = false
        res.data.code === 1 && this.$message.success(res.data.msgZ)
      }
    }
  }
</script>
<style lang="scss">
  .admin{
    &{max-width:800px;text-align:center;}
  }
</style>
